﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using Amortyzacje.Model;

namespace Amortyzacje
{
    class Program
    {
        private static DateTime CurrentDate = DateTime.Today;

        static void Main(string[] args)
        {
            var timer = new Timer(Settings.Default.HourInterval*1000*3600);
            timer.Elapsed += Timer_Elapsed;

            Console.WriteLine("Amortyzacje w toku...");

            Console.ReadLine();
        }

        private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if(DateTime.Today.Day!=1)
                return;
            CurrentDate = DateTime.Today;
            var assets =
                gm_srodki_trwale.Find(
                    x => x.umorzenie < x.wartosc && x.zlikwidowany.HasValue ? x.zlikwidowany == 0 : false);
            var kst = gm_kst.All().ToList();
            foreach (var asset in assets)
            {
                var newAmo = new gm_srodki_trwale_amortyzacje
                                 {
                                     data_amortyzacji = CurrentDate.ToShortDateString(),
                                     nr_ewidencyjny = asset.nr_ewidencyjny,
                                     kwota = asset.wartosc.Value*kst.First(k => k.kod == asset.KST_id.Value).stawka
                                 };
                newAmo.Save();
                asset.umorzenie += newAmo.kwota;
                asset.Save();
            }
        }
    }
}
